package config

import (
	"fmt"
	"github.com/hashicorp/hcl/v2"
)

type Stack struct {
	Name   string   `hcl:",label" cty:"name"`
	Source []string `hcl:"source" cty:"source"`
	Remain hcl.Body `hcl:",remain"`
}

type Stacks struct {
	Default string   `hcl:"default,optional" cty:"default"`
	Stacks  []*Stack `hcl:"stack,block" cty:"stack"`
	Remain  hcl.Body `hcl:",remain"`
}

func (sts *Stacks) Get(stack string) (*Stack, error) {
	for _, s := range sts.Stacks {
		if s.Name == stack {
			return s, nil
		}
	}
	return nil, fmt.Errorf("stack %s not found", stack)
}

func (sts *Stacks) Has(stack string) bool {
	for _, s := range sts.Stacks {
		if s.Name == stack {
			return true
		}
	}
	return false
}

func (sts *Stacks) Names() []string {
	names := make([]string, len(sts.Stacks))
	for i, stack := range sts.Stacks {
		names[i] = stack.Name
	}
	return names
}
